package com.bootdo.check.controller;

import com.bootdo.check.domain.CountData;
import com.bootdo.check.domain.VideoDO;
import com.bootdo.check.service.VideoService;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 视频记录表
 *
 * @author sean
 * @email 1992lcg@163.com
 * @date 2018-07-28 14:44:44
 */

@Controller
@RequestMapping("/check/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Value("${excelFile}")
    private String filePath;

    @GetMapping()
    @RequiresPermissions("check:video:video")
    String Video() {
        return "check/video/video";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("check:video:video")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<VideoDO> videoList = videoService.list(query);
        int total = videoService.count2(query);
        PageUtils pageUtils = new PageUtils(videoList, total);
        return pageUtils;
    }
    
    @ResponseBody
    @GetMapping("/count")
    @RequiresPermissions("check:video:video")
    public CountData count() {
    	CountData cd = new CountData();
    	Query query = null;
    	Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("offset", 0);
        map.put("limit", 10); 
    	map.put("status", "待检测");
    	query = new Query(map);
    	cd.setWait(videoService.count(query));
    	map.put("status", "检测中");
    	query = new Query(map);
    	cd.setChecking(videoService.count(query));
    	map.put("status", "下载完成");
    	query = new Query(map);
    	cd.setDowned(videoService.count(query));
    	map.put("status", "开始下载");
    	query = new Query(map);
    	cd.setDowning(videoService.count(query));
    	map.put("status", "检测不合格");
    	query = new Query(map);
    	cd.setFailed(videoService.count(query));
    	map.put("status", "检测合格");
    	query = new Query(map);
    	cd.setSuccess(videoService.count(query));
    	map.put("status", "检测异常");
    	query = new Query(map);
    	cd.setException(videoService.count(query));
    	
    	return cd;
    }

    @GetMapping("/add")
    @RequiresPermissions("check:video:add")
    String add() {
        return "check/video/add";
    }

    @GetMapping("/edit/{idvideo}")
    @RequiresPermissions("check:video:edit")
    String edit(@PathVariable("idvideo") Integer idvideo, Model model) {
        VideoDO video = videoService.get(idvideo);
        model.addAttribute("video", video);
        return "check/video/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("check:video:add")
    public R save(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        boolean uploadSuccess = false;
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, fileName);
            uploadSuccess = true;
        } catch (Exception e) {
            return R.error();
        }
        if (uploadSuccess == true) {
            String sheetName;
            Map<String, List<List<String>>> excelContent = ExcelUtils.read(filePath + fileName);
            for (Map.Entry<String, List<List<String>>> sheetContent : excelContent.entrySet()) {
                sheetName = sheetContent.getKey();
                for (List<String> v : sheetContent.getValue()) {
                    VideoDO video = new VideoDO();
                    video.setTitle(v.get(1));
                    video.setUrl(v.get(2));
                    video.setLength(Integer.parseInt(v.get(3)));
                    video.setViews(Integer.parseInt(v.get(4)));
                    video.setIsOriginal(v.get(5));
                    video.setFielname(fileName);
                    video.setSheetname(sheetName);
                    video.setStatus("待检测");
                    videoService.save(video);
                }
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("check:video:edit")
    public R update(VideoDO video) {
        videoService.update(video);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("check:video:remove")
    public R remove(Integer idvideo) {
        if (videoService.remove(idvideo) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("check:video:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] idvideos) {
        videoService.batchRemove(idvideos);
        return R.ok();
    }

}
